package com.hzzh.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzzh.service.DepartmentService;

/**
 * Servlet implementation class AddDeleteDepartmentServlet
 */
@WebServlet("/AddDeleteDepartmentServlet")
public class AddDeleteDepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddDeleteDepartmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		String code = request.getParameter("code");
		DepartmentService service = new DepartmentService();
		
		try {
			if(code != null && code.equals("add")){
				service.addDepartmentService(request.getParameter("departmentname"));
			}
			if(code != null && code.equals("delete")){
				service.deleteDepartmentService(Integer.parseInt(request.getParameter("departmentid")));
			}
			request.getRequestDispatcher("ViewAllDepartmentsServlet?code=viewalldepartments").forward(request, response);
		}
		catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}

}
